package com.example.easybuy.controller;


import com.example.easybuy.pojo.EasybuyOrder;
import com.example.easybuy.pojo.EasybuyProduct;
import com.example.easybuy.service.EasybuyOrderService;
import com.example.easybuy.service.EasybuyProductService;
import org.apache.velocity.util.ArrayListWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 张子晨
 * @since 2021-03-12
 */
@RestController
@RequestMapping("/easybuyOrder")
public class EasybuyOrderController {

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    EasybuyOrderService easybuyOrderService;

    @Autowired
    EasybuyProductService easybuyProductService;

    @RequestMapping("/addOrder")
    public int addOrder(@RequestParam("userId") int userId,
                        @RequestParam("loginName") String loginName,
                        @RequestParam("userAddress") String userAddress,
                        @RequestParam("cost") String cost,
                        @RequestParam("serialNumber") String serialNumber) {
        Map<String, Object> map = new HashMap<>();
        map.put("userId", userId);
        map.put("loginName", loginName);
        map.put("userAddress", userAddress);
        map.put("cost", Integer.parseInt(cost));

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        map.put("createTime", sdf.format(new Date()));

        map.put("serialNumber", serialNumber);
        easybuyOrderService.addOrder(map);
        return 1;
    }

    @RequestMapping("/selectOrderBySerialNumber")
    public EasybuyOrder selectOrderBySerialNumber(String serialNumber, String userId) {

        //获取Redis中的购物车
        Map<String, Object> redisMap = (Map<String, Object>) redisTemplate.opsForValue().get("car" + userId);

        Map<String, Integer> stockMap = (Map<String, Integer>) redisMap.get("stockMap");

        //减少商品库存
        for (String key : stockMap.keySet()) {
            int stock = easybuyProductService.getProductById(Integer.parseInt(key)).getStock();
            int finalStock = stock - stockMap.get(key);
            if (finalStock > 0) {
                easybuyProductService.reduceProduct(finalStock, Integer.parseInt(key));
            } else {
                return null;
            }
        }
//        redisTemplate.delete(redisTemplate.keys("car"+userId));
//        redisTemplate.delete(redisTemplate.keys("buyCar"+userId));

        return easybuyOrderService.selectOrderBySerialNumber(serialNumber);
    }

    //根据用户id查询全部订单
    @RequestMapping("/getAllOrderByUserId")
    private List<EasybuyOrder> getAllOrderByUserId(String userId) {

        System.out.println("----------------------" + easybuyOrderService.getAllOrderByUserId(Integer.parseInt(userId)));
        return easybuyOrderService.getAllOrderByUserId(Integer.parseInt(userId));
    }

}

